<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_pengumuman extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'content_id';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_pt = isset($_POST['kd_pt']) ? strval($_POST['kd_pt']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.*, b.category');
			$this->db->from('portal_content a');
			$this->db->join('portal_category b', 'b.category_id = a.category_id');
			$this->db->where('a.category_id', "1");
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('portal_content');
		$this->db->where('content_id', $kode);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('category_id', $data['category_id']);
		$this->db->set('content_title', $data['content_title']);
		$this->db->set('content', $data['content']);
		$this->db->set('summary', $data['summary']);
		$this->db->set('url', $data['url']);
		$this->db->set('date_post', FormatDateToMysql($data['date_post']));		
		$this->db->set('published', $data['published']);
		
		$result = $this->db->insert('portal_content');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('category_id', $data['category_id']);
		$this->db->set('content_title', $data['content_title']);
		$this->db->set('content', $data['content']);
		$this->db->set('summary', $data['summary']);
		$this->db->set('url', $data['url']);
		$this->db->set('date_post', FormatDateToMysql($data['datepost']));		
		$this->db->set('published', $data['published']);		
		
		$this->db->where('content_id', $data['kode']);
		$result = $this->db->update('portal_content');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		$this->db->flush_cache();
		$this->db->where('content_id', $kode);
		$result = $this->db->delete('portal_content'); 
		
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
}

?>